package conf

import (
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"path/filepath"
)

type Setting struct {
	DataFeed struct {
		Name       string `yaml:"name"`
		Url        string `yaml:"url"`
		DB         string `yaml:"db"`
		Collection string `yaml:"collection"`
		UserName   string `yaml:"username"`
		Password   string `yaml:"password"`
	} `yaml:"data_feed"`
}

// LoadSetting 加载setting配置
func LoadSetting() *Setting {
	setting := new(Setting)
	//从固定目录读取yaml文件
	yamlFile, err := ioutil.ReadFile(SETTINGPATH)
	if err != nil {
		//如果不存在文件 则从相对目录读取
		path := "../conf/files/setting.yml"
		path, _ = filepath.Abs(path)
		yamlFile, _ = ioutil.ReadFile(path)
	}

	err = yaml.Unmarshal(yamlFile, setting)
	if err != nil {
		panic(err)
	}
	return setting
}
